package sz.com.postoffice.jxyzwgcase.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sz.com.postoffice.jxyzwgcase.core.PageDomain;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.core.TableSupport;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.service.BaseService;
import sz.com.postoffice.jxyzwgcase.utils.SqlUtil;
import sz.com.postoffice.jxyzwgcase.utils.StringUtils;

import java.util.List;


/**
 *
* @Title: BaseServiceImpl
* @Description:
* 数据层公共实现类
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {
	private static final Logger logger= LoggerFactory.getLogger(BaseServiceImpl.class);

/*
	@Resource
	BaseDao<T> baseDao;
*/

	protected abstract BaseDao<T> getMapper();

	@Override
	public boolean insert(T entity)  {
		boolean falg=false;
		try {
			getMapper().insert(entity);
			falg=true;
		} catch (Exception e) {
			logger.error("新增"+getClassName(entity)+"失败!原因是:",e);
		}
		return falg;
	}


	@Override
	public boolean update(T entity){
		boolean falg=false;
		try {
			 getMapper().update(entity);
			 falg=true;
		} catch (Exception e) {
			logger.error("更新"+getClassName(entity)+"失败!原因是:",e);
		}
		return falg;
	}

	@Override
	public boolean deleteByPrimaryKey(int id)  {
		boolean falg=false;
		try {
			 getMapper().deleteByPrimaryKey(id);
			 falg=true;
		} catch (Exception e) {
			logger.error("id:"+id+"删除失败!原因是:",e);
		}
		return falg;
	}

	@Override
	public boolean delete(T entity){
		boolean falg=false;
		try {
			getMapper().delete(entity);
			falg=true;
		} catch (Exception e) {
			logger.error("删除"+getClassName(entity)+"失败!原因是:",e);
		}
		return falg;
	}

	@Override
	public T findByPrimaryKey(int id) {
		T obj = null;
		try {
			obj = getMapper().findByPrimaryKey(id);
		} catch (Exception e) {
			logger.error("id:"+id+"查询失败!原因是:",e);
		}
		return obj;
	}

	@Override
	public T findByEntity(T entity) {
		T obj = null;
		try {
			obj = getMapper().findByEntity(entity);
		} catch (Exception e) {
			logger.error("查询"+getClassName(entity)+"失败!原因是:",e);
		}
		return obj;
	}

	@Override
	public TableDataInfo findByListEntity(T entity) {
		List<T> list = null;
		try {
			Page<?> page =startPage();
			logger.info(entity.getClass().getName()+"设置第{}页{}条数据!",page.getPageNum(),page.getPageSize());
			list = getMapper().findByListEntity(entity);
			if (list != null) {
				logger.info("总共有:"+page.getTotal()+"条数据,实际返回:"+list.size()+"条数据!");

			}
		} catch (Exception e) {
			logger.error("查询"+getClassName(entity)+"失败!原因是:",e);
		}
		TableDataInfo dataTable = getDataTable(list);
		return dataTable;
	}

	@Override
	public List<T> findAll() {
		List<T> list = null;
		try {
			list =  getMapper().findAll();
		} catch (Exception e) {
			logger.error("查询失败!原因是:",e);
		}
		return list;
	}

	@Override
	public Object findByObject(Object obj) {
		Object result = null;
		try {
			result = getMapper().findByObject(obj);
		} catch (Exception e) {
			logger.error("查询"+obj+"失败!原因是:",e);
		}
		return result;
	}

	private String getClassName(T t){

		return t.getClass().getName();
	}
	/**
	 * 设置请求分页数据
	 */
	protected Page<?>  startPage()
	{
		PageDomain pageDomain = TableSupport.buildPageRequest();
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
		{
			String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
			return PageHelper.startPage(pageNum, pageSize, orderBy);
		}
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected TableDataInfo getDataTable(List<?> list)
	{
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(200);
		rspData.setRows(list);
		if (list == null) {
			rspData.setTotal(0);
		} else {
			rspData.setTotal(new PageInfo(list).getTotal());
		}
		rspData.setMsg("请求成功");
		return rspData;
	}
}
